package com.czl.module_service.fragment.assetUse

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.MoveSortBean
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.asset.MyAssetStoreApplySortListAdapter
import com.czl.module_service.databinding.FragmentMyAssetStoreApplyDetailBinding
import com.czl.module_service.databinding.FragmentMyAssetStoreApplyDetailFooterBinding
import com.czl.module_service.databinding.FragmentMyAssetStoreApplyDetailHeaderBinding
import com.czl.module_service.fragment.approvalFlow.ApprovalFlowFragment
import com.czl.module_service.viewmodel.assetUse.MyAssetStoreApplyDetailViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_STORE_APPLY_DETAIL)
class MyAssetStoreApplyDetailFragment :
    ApprovalFlowFragment<FragmentMyAssetStoreApplyDetailBinding, MyAssetStoreApplyDetailViewModel>() {

    private val footerBinding: FragmentMyAssetStoreApplyDetailFooterBinding by lazy {
        FragmentMyAssetStoreApplyDetailFooterBinding.inflate(layoutInflater).apply {

        }
    }

    private val mAdapter: MyAssetStoreApplySortListAdapter by lazy {
        MyAssetStoreApplySortListAdapter(this).apply {
            val headerBinding = FragmentMyAssetStoreApplyDetailHeaderBinding.inflate(layoutInflater)
            headerBinding.setVariable(initVariableId(), viewModel)
            addHeaderView(headerBinding.root)
            footerBinding.setVariable(initVariableId(), viewModel)
            addFooterView(footerBinding.root)
        }
    }

    override fun initContentView(): Int {
        return R.layout.fragment_my_asset_store_apply_detail
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.moveId =
            arguments?.getString("moveId")?.toInt()
        viewModel.tabIndex =
            arguments?.getString("tabIndex")?.toInt()!!
        viewModel.taskUserId =
            arguments?.getString("taskUserId")
        viewModel.tvTitle.set("调拨详情")
        viewModel.getMoveInfo(moveId = viewModel.moveId)
        initAdapter()
    }

    private fun initAdapter() {
        binding.ryCommon.run {
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            if (data.status != 3 && data.status != 6) {
                super.initViewObservable()
            }
            if (viewModel.tabIndex == 1 && data.aduitStatus == 0) {
                footerBinding.llBtn1.visibility = View.VISIBLE
                footerBinding.llBtn2.visibility = View.GONE
            } else {
                footerBinding.llBtn1.visibility = View.GONE
//                footerBinding.llBtn2.visibility = View.GONE
            }
            initFlowData(5, viewModel.moveId!!, viewModel.taskUserId!!)
            viewModel.setData(data)
            mAdapter.setList(data.sortList as MutableList<MoveSortBean>?)
        })
        viewModel.uc.backCompleteEvent.observe(this, {
            back()
        })
    }

    override fun getAdapter(): BaseQuickAdapter<*, *> {
        return mAdapter
    }

    override fun dataResult(processStatus: Int?, taskStatus: Int?) {
        if (viewModel.tabIndex == 2 && taskStatus == 2) {
            footerBinding.llBtn1.visibility = View.GONE
            footerBinding.llBtn2.visibility = View.VISIBLE
        } else {
//            footerBinding.llBtn1.visibility = View.GONE
            footerBinding.llBtn2.visibility = View.GONE
        }
    }

}